package com.bristua.barcode.order.biz;

import com.bristua.barcode.common.exception.BizException;
import com.bristua.barcode.customer.dao.TCustomerBindSupplierMapper;
import com.bristua.barcode.customer.dao.TCustomerMapper;
import com.bristua.barcode.customer.enums.CustomerCode;
import com.bristua.barcode.customer.model.TCustomer;
import com.bristua.barcode.customer.model.TCustomerBindSupplier;
import com.bristua.barcode.customer.model.condition.TCustomerBindSupplierExample;
import com.bristua.barcode.customer.model.condition.TCustomerExample;
import com.bristua.barcode.order.dao.TPayOrderMapper;
import com.bristua.barcode.order.model.TPayOrder;
import com.bristua.barcode.supplier.dao.TQrCodeMapper;
import com.bristua.barcode.supplier.model.TQrCode;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/8/10 0010.
 */
@Service
public class GetCustomerOrderQrcodeBiz {
    
    @Resource
    private TPayOrderMapper tPayOrderMapper;

    @Resource
    private TCustomerMapper tCustomerMapper;

    @Resource
    private TQrCodeMapper tQrCodeMapper;

    @Resource
    private TCustomerBindSupplierMapper tCustomerBindSupplierMapper;

    public String getCustomerOrderQRcodeUrl(String orderId){
        TPayOrder tPayOrder = tPayOrderMapper.selectByPrimaryKey(orderId);
        if(tPayOrder!=null){
            String supplierId = tPayOrder.getSupplierId();
            String customerNo = tPayOrder.getCustomerNo();
            TCustomerExample tCustomerExample = new TCustomerExample();
            tCustomerExample.createCriteria().andCustomerNoEqualTo(customerNo);
            List<TCustomer> tCustomers = tCustomerMapper.selectByExample(tCustomerExample);
            if(tCustomers==null || tCustomers.size()==0){
                throw new BizException(CustomerCode.CUSTOMER_IS_NOT_EXIST.getCode(), CustomerCode.CUSTOMER_IS_NOT_EXIST.getMsg());
            }

            TCustomerBindSupplierExample tCustomerBindSupplierExample = new TCustomerBindSupplierExample();
            tCustomerBindSupplierExample.createCriteria().andCustomerIdEqualTo(tCustomers.get(0).getCustomerId()).andSupplierIdEqualTo(supplierId);
            List<TCustomerBindSupplier> tCustomerBindSuppliers = tCustomerBindSupplierMapper.selectByExample(tCustomerBindSupplierExample);
            
            if(tCustomerBindSuppliers!=null && tCustomerBindSuppliers.size()>0){
                TCustomerBindSupplier tCustomerBindSupplier = tCustomerBindSuppliers.get(0);
                String qrCodeId = tCustomerBindSupplier.getQrCodeId();
                TQrCode tQrCode = tQrCodeMapper.selectByPrimaryKey(qrCodeId);
                if(tQrCode!=null){
                    return tQrCode.getQrCodeUrl();
                }
            }
        }
        return null;
    }
}
